Skip to main content
Get up and running with PVAC-HFHE by building a simple application that encrypts data, performs homomorphic operations, and decrypts the results.

Prerequisites

Before you begin, ensure you have:
  • C++17 compatible compiler (GCC 9+, Clang 10+, or MSVC 2019+)
  • PVAC-HFHE cloned and included in your project (see Installation)

Build your first FHE application

1

Create your source file

Create a file named first_fhe.cpp:
2

Generate cryptographic keys

Add key generation code. This creates the public key for encryption and secret key for decryption:
Key generation takes ~859 ms. In production, you’d generate keys once and reuse them.
3

Encrypt values

Encrypt two numbers using the public key and secret key:
Fresh ciphertexts are compact (~42 KB). Encryption takes ~84 ms per value.
4

Perform homomorphic operations

Compute on encrypted data without decrypting it. The server can perform these operations without seeing the plaintext:
Privacy preserved: The server never sees 42 or 17, only encrypted ciphertexts!
5

Decrypt and verify results

The client decrypts the results using their secret key:
6

Compile and run

Compile with C++17 and optimization flags:
Expected output:

Understanding the workflow

1

Key generation (one-time setup)

The client generates a key pair:
  • Public key (pk): Used for encryption and homomorphic operations (can be shared publicly)
  • Secret key (sk): Used for decryption (must be kept private)
2

Encryption (client-side)

The client encrypts sensitive data using both pk and sk, producing a ciphertext that reveals nothing about the plaintext.
3

Homomorphic computation (server-side)

The server performs operations on encrypted data using only the public key. It never sees the plaintext values.
4

Decryption (client-side)

The client decrypts the result using their secret key to reveal the computed value.

Try a more complex example

Polynomial evaluation

Evaluate f(x) = x³ + 2x² + 3x + 4 at x = 5, entirely on encrypted data:
Expected result: 5³ + 2(5²) + 3(5) + 4 = 125 + 50 + 15 + 4 = 194

Text encryption

PVAC-HFHE also supports text encryption via automatic packing:

Performance considerations

Best for:
  • Scalar arithmetic: 2.9-14.3× faster than RLWE schemes (BFV/CKKS)
  • Small circuit depth (d ≤ 2): Outperforms all schemes
  • Addition-heavy workloads: 10-87× faster than RLWE
  • Compact ciphertexts: 6-85× smaller than RLWE
  • Simple ML inference: Privacy-preserving predictions
Not ideal for:
  • Deep circuits (d ≥ 3): RLWE schemes outperform due to modulus switching
  • SIMD/batching: BFV is 146× faster for batch operations
  • Very deep ML models: Consider CKKS for deep neural networks
Ciphertext size grows exponentially with depth (~3× per multiplication level).
  • Use ct_square(pk, a) instead of ct_mul(pk, a, a) for squaring
  • Use ct_mul_const() and ct_add_const() when multiplying/adding by public constants
  • Minimize circuit depth by factoring and reusing intermediate results
  • Use compiler flags: -O2 -march=native for SIMD acceleration

Next steps

Core concepts

Understand the hypergraph-based encryption and LPN security

Guides

Learn advanced techniques for key generation, depth management, and optimization

Examples

Explore complete working examples including ML inference

API reference

Browse the complete API documentation